<template>
	<view>
		<component-popup :propShow="popup_status" propPosition="bottom" @onclose="popup_close_event">
			    <view >
					<block>
					    <view class="nav-search padding-horizontal-main bg-white" style="padding-top: 8px;">
							<component-search @onsearch="search_button_event" :propIsOnEvent="true" :propIsRequired="false" propPlaceholder="输入商品名称搜索"></component-search>
					    </view>
					</block>

					<!-- 分类内容 -->
					<view v-if="category_list.length > 0" :class="'category-content pr bs-bb '+(category_show_level == 0 ? 'goods-model' : '')" :style="'height:calc(100vh - '+(status_bar_height+48)+'px);'">
					    <block v-if="category_show_level == 1">
					        <!-- 一级模式 -->
					        <view class="model-one padding-sm oh">
					            <block v-for="(item, index) in category_list" :key="index">
					                <view class="content-item padding-sm tc cp" :data-value="item.id" @tap="category_event">
					                    <view class="content auto bg-white wh-auto border-radius-main">
					                        <image v-if="(item.icon || null) != null" :src="item.icon" mode="aspectFit" class="icon radius"></image>
					                        <view class="text single-text">{{item.name}}</view>
					                    </view>
					                </view>
					            </block>
					        </view>
					    </block>
					    <block v-else>
					        <!-- 商品列表模式 -->
					        <block v-if="category_show_level == 0">
					            <!-- 一级导航 -->
					            <view class="top-nav bg-white wh-auto pa br-b scroll-view-horizontal">
					                <scroll-view :scroll-x="true" :scroll-with-animation="true" :scroll-into-view="'one-nav-item-'+nav_active_index" class="ht-auto">
					                    <block v-for="(item, index) in category_list" :key="index">
					                        <view :class="'text-size-sm item tc cr-base cp dis-inline-block ' + (nav_active_index == index ? 'cr-main border-color-main fw-b' : '')" :id="'one-nav-item-'+index" :data-index="index" :data-itemtwoindex="-1" :data-itemthreeindex="-1" @tap="nav_event">
					                            <view :class="'icon-content circle br auto ' + (nav_active_index == index ? 'border-color-main' : '')">
					                                <image  :src="((item[category_goods_model_icon_field] || null) == null) ? common_static_url+'images.png' : item[category_goods_model_icon_field]" mode="aspectFit" class="icon dis-block auto wh-auto ht-auto circle"></image>
					                            </view>
					                            <view class="margin-top-xs">{{item.name}}</view>
					                        </view>
					                    </block>
					                </scroll-view>
					            </view>
					            <!-- 二级导航 -->
					            <view v-if="category_one_subset_count > 0" class="left-nav bg-white ht-auto">
					                <scroll-view :scroll-y="true" class="ht-auto">
					                    <view :class="((common_site_type != 1) ? 'left-content-actual' : '')">
					                        <view :class="'text-size-sm item tc cr-base cp oh ' + (nav_active_item_two_index == -1 ? 'nav-active cr-main border-color-main' : '')" :data-index="nav_active_index" :data-itemtwoindex="-1" :data-itemthreeindex="-1" @tap="nav_event">
					                            <text>全部</text>
					                        </view>
					                        <block v-if="(data_content || null) != null && (data_content.items || null) != null && data_content.items.length > 0">
					                            <block v-for="(item, index) in data_content.items" :key="index">
					                                <view :class="'text-size-sm item tc cr-base cp oh ' + (nav_active_item_two_index == index ? 'nav-active cr-main border-color-main' : '')" :data-index="nav_active_index" :data-itemtwoindex="index" :data-itemthreeindex="-1" @tap="nav_event">
					                                    <text>{{item.name}}</text>
					                                </view>
					                            </block>
					                        </block>
					                    </view>
					                </scroll-view>
					            </view>
					            <!-- 商品列表 -->
					            <view :class="'goods-right-content pa bs-bb padding-top-main padding-horizontal-main '+(category_one_subset_count > 0 ? '' : 'category-one-subset-content')">
					                <scroll-view :scroll-y="true" class="ht-auto goods-list" :scroll-top="scroll_top" @scroll="scroll_event" @scrolltolower="scroll_lower" lower-threshold="60">
					                    <view :class="((common_site_type != 1) ? 'right-content-actual' : '')+' pr'">
					                        <!-- 三级导航 -->
					                        <view v-if="(data_three_content || null) != null && (data_three_content.items || null) != null && data_three_content.items.length > 0" class="word-list scroll-view-horizontal">
					                            <scroll-view :scroll-x="true" :scroll-with-animation="true" :scroll-into-view="'three-nav-item-'+nav_active_item_three_index">
					                                <view :class="'word-icon dis-inline-block text-size-sm round padding-top-xs padding-bottom-xs padding-left padding-right '+((nav_active_item_three_index == -1) ? 'bg-main-light br-main-light cr-main' : 'br-gray cr-gray')" :data-index="nav_active_index" :data-itemtwoindex="nav_active_item_two_index" :data-itemthreeindex="-1" @tap="nav_event">全部</view>
					                                <block v-for="(item, index) in data_three_content.items" :key="index">
					                                    <view :class="'word-icon dis-inline-block text-size-sm round padding-top-xs padding-bottom-xs padding-left padding-right '+((nav_active_item_three_index == index) ? 'bg-main-light br-main-light cr-main' : 'br-gray cr-gray')" :id="'three-nav-item-'+index" :data-index="nav_active_index" :data-itemtwoindex="nav_active_item_two_index" :data-itemthreeindex="index" @tap="nav_event">{{item.name}}</view>
					                                </block>
					                            </scroll-view>
					                        </view>
					                        <!-- 右侧商品列表 -->
					                        <view v-if="(data_list || null) != null && data_list.length > 0" class="oh">
					                            <view v-for="(item, index) in data_list" :key="index" class="item bg-white border-radius-main oh pr spacing-mb">
					                                <!-- 商品主体内容 -->
					                                <view :data-value="item.goods_url+'&is_opt_back=1'" >
														<!-- :src="item.images" -->
														<view v-if="item.images === null">
															<image :src="'https://zkerp.oss-cn-shenzhen.aliyuncs.com/public/'+item.name+'.png'"  mode="widthFix" style="border: 1px solid bisque;margin:4px" class="goods-img radius fl"></image>
														</view>
														<view v-else>
															<image :src="item.images"  mode="widthFix" style="border: 1px solid bisque;margin:4px" class="goods-img radius fl"></image>
														</view>

														<view class="goods-base padding-top-sm padding-right-sm fr">
					                                        <view class="goods-base-content">
					                                            <view class="goods-title text-size-sm multi-text">{{item.title}}</view>
					                                            <view v-if="(item.simple_desc || null) != null" class="simple-desc text-size-xs margin-top-sm single-text">
																规格:<span class="cr-red">{{item.simple_desc}}</span></view>
																<view class="goods-title text-size-sm multi-text">
																	<span class="cr-red">{{currency_symbol}}{{item.min_price}}</span>/{{item.inventory_unit}}
																</view>
					                                        </view>
					                                        <view class="margin-top-sm oh">
					                                            <view class="sales-price text-size-sm single-text pa"></view>
					                                            <view v-if="common_site_type != 1" class="buy-opt tc pa">
					                                                <block v-if="(item.is_error || 0) == 0">
					                                                    <view v-if="item.buy_number > 0" class="dis-inline-block va-m cp" :data-index="index" data-type="0" @tap.stop="buy_number_event">
					                                                        <uni-icons type="minus" size="46rpx" color="#f00"></uni-icons>
					                                                    </view>
					                                                    <view  v-if="item.buy_number > 0" class="buy-number dis-inline-block cr-base text-size-sm padding-left-xs padding-right-xs va-m">
																			<input disabled="true" type="digit" @click="openKeyBoard" @blur="buy_number_input_event" :id="item.id" style="width: 50px;background-color: beige;margin: 0 10px;" class="cr-base" :value="item.buy_number" />
																		</view>

					                                                    <view class="dis-inline-block va-m cp" :data-index="index" data-type="1" @tap.stop="buy_number_event">
					                                                        <uni-icons type="plus" size="46rpx" color="#1AAD19"></uni-icons>
					                                                    </view>
					                                                </block>
					                                                <block v-else>
					                                                    <text class="cr-grey text-size-xs">{{item.error_msg}}</text>
					                                                </block>
					                                            </view>
					                                        </view>
					                                    </view>
					                                </view>
					                                <!-- 标签插件 -->
				<!-- 	                                <view v-if="(plugins_label_data || null) != null && plugins_label_data.data.length > 0" :class="'plugins-label oh pa plugins-label-'+((plugins_label_data.base.is_user_goods_label_icon || 0) == 0 ? 'text' : 'img')+' plugins-label-'+(plugins_label_data.base.user_goods_show_style || 'top-left')">
					                                    <block v-for="(lv,li) in plugins_label_data.data" :key="li">
					                                        <view v-if="(lv.goods_ids || null) != null && lv.goods_ids.indexOf(item.id) != -1" class="lv dis-inline-block va-m" :data-value="((plugins_label_data.base.is_user_goods_label_url || 0) == 1) ? (lv.url || '') : ''" @tap="url_event">
					                                            <view v-if="(plugins_label_data.base.is_user_goods_label_icon || 0) == 0" class="round cr-white bg-main text-size-xs fl" :style="((lv.bg_color || null) != null ? 'background-color:'+ lv.bg_color+' !important;' : '')+((lv.text_color || null) != null ? 'color:'+ lv.text_color+' !important;' : '')">{{lv.name}}</view>
					                                            <image v-else class="dis-block" :src="lv.icon" mode="scaleToFill"></image>
					                                        </view>
					                                    </block>
					                                </view> -->
					                            </view>
					                        </view>
					                        <block v-else>
					                            <component-no-data :propStatus="data_list_loding_status" :propMsg="data_list_loding_msg"></component-no-data>
					                        </block>
					                    </view>
					                </scroll-view>
					            </view>
					        </block>
					        <block v-else>
					            <!-- 一级导航 -->
					            <view class="left-nav bg-white ht-auto">
					                <scroll-view :scroll-y="true" class="ht-auto">
					                    <block v-for="(item, index) in category_list" :key="index">
					                        <view :class="'text-size-sm item tc cr-base cp oh ' + (nav_active_index == index ? 'nav-active cr-main border-color-main' : '')" :data-index="index" :data-itemindex="-1" @tap="nav_event">
					                            <text>{{item.name}}</text>
					                        </view>
					                    </block>
					                </scroll-view>
					            </view>
					            <view class="right-container pa">
					                <scroll-view :scroll-y="true" class="ht-auto">
					                    <!-- 一级内容基础容 -->
					                    <view v-if="(data_content || null) != null" class="padding-top-main padding-horizontal-main oh">
					                        <!-- 一级基础信息 -->
					                        <view v-if="(data_content.vice_name || null) != null || (data_content.describe || null) != null" class="one-content bg-white padding-main border-radius-main cp spacing-mb" :data-value="data_content.id" @tap="category_event">
					                            <view v-if="(data_content.vice_name || null) != null" class="text-size fw-b" :style="'color:' + data_content.bg_color + ';'">{{data_content.vice_name}}</view>
					                            <view v-if="(data_content.describe || null) != null" class="cr-grey margin-top-sm">{{data_content.describe}}</view>
					                        </view>
					                        <!-- 一二级数据渲染 -->
					                        <block v-if="(data_content.items || null) != null && data_content.items.length > 0">
					                            <!-- 二级模式 -->
					                            <block v-if="category_show_level == 2">
					                                <view class="two-content bg-white oh padding-main border-radius-main spacing-mb">
					                                    <block v-for="(v, index) in data_content.items" :key="index">
					                                        <view class="content-item padding-sm tc cp" :data-value="v.id" @tap="category_event">
					                                            <view class="content wh-auto">
					                                                <image v-if="(v.icon || null) != null" :src="v.icon" mode="aspectFit" class="icon radius"></image>
					                                                <view class="text single-text">{{v.name}}</view>
					                                            </view>
					                                        </view>
					                                    </block>
					                                </view>
					                            </block>
					                            <!-- 三级模式 -->
					                            <block v-if="category_show_level == 3">
					                                <block v-for="(v, index) in data_content.items" :key="index">
					                                    <view class="spacing-nav-title">
					                                        <text class="text-wrapper text-size-md">{{v.name}}</text>
					                                        <text v-if="(v.describe || null) != null" class="vice-name margin-left-lg cr-gray">{{v.describe}}</text>
					                                        <view :data-value="v.id" @tap="category_event" class="arrow-right padding-right-xxxl cr-gray fr cp">更多</view>
					                                    </view>
					                                    <view v-if="(v.items || null) != null && v.items.length > 0" class="bg-white oh padding-main border-radius-main spacing-mb">
					                                        <block v-for="(vs, index2) in v.items" :key="index2">
					                                            <view class="content-item padding-sm tc cp" :data-value="vs.id" @tap="category_event">
					                                                <view class="content wh-auto">
					                                                    <image v-if="(vs.icon || null) != null" :src="vs.icon" mode="aspectFit" class="icon radius"></image>
					                                                    <view class="text single-text">{{vs.name}}</view>
					                                                </view>
					                                            </view>
					                                        </block>
					                                    </view>
					                                </block>
					                            </block>
					                        </block>
					                        <block v-else>
					                            <!-- 提示信息 -->
					                            <component-no-data propStatus="0" propMsg="没有子分类数据"></component-no-data>
					                        </block>
					                    </view>
					                    <view v-else>
					                        <!-- 提示信息 -->
					                        <component-no-data propStatus="0" propMsg="没有子分类数据"></component-no-data>
					                    </view>
					                </scroll-view>
					            </view>
					        </block>
					    </block>
					</view>

					<view v-if="category_list.length == 0 && data_list_loding_status != 0">
					    <!-- 提示信息 -->
					    <component-no-data :propStatus="data_list_loding_status"></component-no-data>
					</view>
					
					<!-- 数字键盘 -->
					<block v-if="keyboard_status">
						                    <view class="cart-mask wh-auto ht-auto pf" @tap="keyboard_event"></view>
						                    <view class="cart-content bg-white border-radius-main pa oh">
						                        <block>
						                            <view class="oh br-b padding-vertical-main padding-horizontal-main">
														<view sclass="input-content">&nbsp;{{ inputValue.join("") }}</view>
														<view class="confirm-btn" @click="confirm">确定</view>
						                            </view>
						                            <scroll-view :scroll-y="true" class="cart-list goods-list">
						                                <view class="item padding-main oh spacing-mb">
						                                    <view>
																  <view class="modal"  :animation="animationData1" @touchmove.stop.prevent>
																	<view class=" content-wrap" :animation="animationData2">
																	  <view class="key-box">
																		<view
																		  :class="{
																			'handel-btn': ['.', '删除', ''].includes(item),
																			'active-btn': activeKey === item,
																			'active-handel-btn':
																			  activeKey === item && ['.', '删除'].includes(item),
																		  }"
																		  v-for="(item, index) in keyboardList"
																		  :key="index"
																		  @click="setValue(item)"
																		  >{{ item }}</view
																		>
																	  </view>
																	</view>
					
																  </view>
															</view>
						                                </view>
						                            </scroll-view>
						                        </block>
						                    </view>
					</block>

					<!-- 购物车抛物线 -->
					<component-cart-para-curve ref="cart_para_curve"></component-cart-para-curve>

					<!-- 仅商品模式展示购物车和规格选择 -->
					<block v-if="common_site_type != 1 && category_show_level == 0">
					                <!-- 购物车列表 -->
					                <block v-if="cart_status">
					                    <view class="cart-mask wh-auto ht-auto pf" @tap="cart_event"></view>
					                    <view class="cart-content bg-white border-radius-main pa oh">
					                        <block v-if="(cart || null) != null && (cart.data || null) != null && cart.data.length > 0">
					                            <view class="oh br-b padding-vertical-main padding-horizontal-main">
					                                <text class="va-m text-size-xs cr-base">已选商品</text>
					                                <view class="fr cp" @tap="cart_all_delete_event">
					                                    <view class="dis-inline-block va-m">
					                                        <uni-icons type="trash" size="12" color="#f00"></uni-icons>
					                                    </view>
					                                    <text class="cr-red va-m text-size-xs margin-left-xs">清空</text>
					                                </view>
					                            </view>
					                            <scroll-view :scroll-y="true" class="cart-list goods-list">
					                                <view v-for="(goods, index) in cart.data" :key="index" class="item padding-main oh spacing-mb">
					                                    <navigator :url="goods.goods_url" hover-class="none">
					<!--                                        <image style="border: 1px solid bisque;" :src="goods.images" mode="widthFix" class="goods-img radius fl br"></image> -->
															<view v-if="goods.images === null">
																<image :src="'https://zkerp.oss-cn-shenzhen.aliyuncs.com/public/'+goods.title+'.png'"  mode="widthFix" style="border: 1px solid bisque;" class="goods-img radius fl br"></image>
															</view>
															<view v-else>
																<image :src="goods.images"  mode="widthFix" style="border: 1px solid bisque;" class="goods-img radius fl"></image>
															</view>
					                                        <view class="goods-base fr">
					                                            <view class="goods-base-content">
					                                                <view class="goods-title text-size-sm single-text">{{goods.title}}</view>
					                                                <view v-if="goods.spec != null" class="text-size-xs cr-grey margin-top-sm">
																		<text>{{goods.spec}}</text>
					                                                </view>
					                                            </view>
					                                            <view class="margin-top-sm oh">
					                                                <view class="sales-price text-size-sm single-text dis-inline-block va-m"><span>{{currency_symbol}}{{goods.price}}/{{goods.unitName}}</span></view>
					                                                <view class="tc fr">
					                                                    <block v-if="goods.is_error == 0">
					                                                        <view v-if="(goods.stock || 0) > 0" class="dis-inline-block va-m cp" :data-index="index" data-type="0" @tap.stop="cart_buy_number_event">
					                                                            <uni-icons type="minus" size="46rpx" color="#f00"></uni-icons>
					                                                        </view>
					                                                        <view v-if="(goods.stock || 0) > 0" class="buy-number dis-inline-block cr-base text-size-sm padding-left-xs padding-right-xs va-m">
																				{{goods.stock}}
																			</view>
					                                                        <view class="dis-inline-block va-m cp" :data-index="index" data-type="1" @tap.stop="cart_buy_number_event">
					                                                            <uni-icons type="plus" size="46rpx" color="#1AAD19"></uni-icons>
					                                                        </view>
					                                                    </block>
					                                                    <block v-else>
					                                                        <text class="cr-red">{{goods.error_msg}}</text>
					                                                    </block>
					                                                </view>
					                                            </view>
					                                        </view>
					                                    </navigator>
					                                </view>
					                            </scroll-view>
					                        </block>
					                        <block v-else>
					                            <component-no-data propStatus="0" propMsg="请先选购商品"></component-no-data>
					                        </block>
					                    </view>
					                </block>
					                <!-- 购物车底部导航 -->
					                <view class="botton-nav round pa bg-main-pair oh">
					                    <view class="cart dis-inline-block va-m margin-left-xxl pr cp" @tap="cart_event">
					                        <uni-icons type="cart" size="32rpx" color="#fff"></uni-icons>
					                        <view v-if="(cart || null) != null && (cart.buy_number || 0) != 0" class="badge-icon pa">
					                            <component-badge :propNumber="cart.buy_number"></component-badge>
					                        </view>
					                    </view>
					                    <view class="cart-total-price single-text dis-inline-block fw-b cr-white va-m margin-left-xl">
					                        <text class="text-size-sm">{{currency_symbol}}</text>
					                        <text class="text-size-lg">{{(cart || null) == null ? 0 : (cart.total_price || 0)}}</text>
					                    </view>
					                    <button type="default" size="mini" hover-class="none"  @tap="buy_submit_event" class="text-size-sm pa radius-0 bg-main cr-white">选好了</button>
					                </view>
					            </block>



				</view>
		</component-popup>
	</view>
</template>

<script>
	const app = getApp();
	import base64 from '../../common/js/lib/base64.js';
	import componentPopup from "../../components/popup/popup";
	import componentSearch from "../../components/search/search";
	import componentNoData from "../../components/no-data/no-data";

	import componentCartParaCurve from '../../components/cart-para-curve/cart-para-curve';

	export default {
		name:"goods-category",
		data() {
			return {
				keyboard_status:false,
				currency_symbol: app.globalData.data.currency_symbol,
				customerId:"",
				activeKey: "",
				inputValue: [],
				user: null,
				popup_status: false,
				data_list_loding_status: 1,
				// 基础配置
				category_show_level: 0,
				category_list: [],
				nav_active_index: 0,
				nav_active_item_two_index: -1,
				nav_active_item_three_index: -1,
				data_three_content: null,
				data_list: [],
				tabbar_params: null,
				category_one_subset_count: 0,
				data_content: null,
				category_list: [],
				common_site_type: 0,
				cart_status: false,
				is_first:1,
				data_page:1,
				data_total:0,
				data_page_total:0,
			};
		},
		components: {
			componentPopup,componentSearch,componentNoData,componentCartParaCurve
		},
		computed: {
					keyboardList() {
					  return this.decimals
						? ["1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "删除"]
						: ["1", "2", "3", "4", "5", "6", "7", "8", "9", ".", "0", "删除"];
					},
		},
		methods:{
			confirm() {
				//console.log("ywid===",this.ywid);
			  let result = Number(this.inputValue.join(""));
			
			  // this.$emit("confirm", result,this.ywid);
			  console.log("----result---",result);
			  var goodqty =result;
			  var goodid =this.ywid;
			
			
			
			  // 为0或减操作则查询
			  var cart_item = null;
			  var type =1;
			  if(goodqty > 0) {
			      var cart_data = this.cart.data;
			      for(var i in cart_data) {
			          if(goodid == cart_data[i]['goods_id']) {
			              cart_item = cart_data[i];
			              break;
			          }
			      }
			  }
			
			  console.log("cart_item",cart_item);
			  				console.log("---goodid---",goodid.trim());
			
			  this.cart_update(cart_item.id,goodid,goodqty);
			
			  this.keyboard_status =false;
			},
			setValue(data) {
			  this.activeKey = data;
			  let timer = setTimeout(() => {
			    this.activeKey = "-";
			    clearTimeout(timer);
			  }, 100);
			
			  if (
			    !data ||
			    (data !== "删除" &&
			      this.inputValue.includes(".") &&
			      this.inputValue.join("").split(".")[1].length === this.pointLength)
			  )
			    return false;
			
			  if (
			    this.inputValue.length === 1 &&
			    this.inputValue[0] === "0" &&
			    !["删除", "."].includes(data)
			  ) {
			    return (this.inputValue = [data]);
			  }
			
			  if (data !== "删除") {
			    if (this.inputValue.includes(".")) {
			      if (
			        this.inputValue.join("").split(".")[0].length === this.maxLength
			      ) {
			        return false;
			      }
			    } else if (this.inputValue.length === this.maxLength) {
			      return false;
			    }
			  }
			
			  if (data === "删除") {
			    return this.inputValue.length && this.inputValue.pop();
			  }
			  if (data === ".") {
			    if (!this.inputValue.length || this.inputValue.includes("."))
			      return false;
			  }
			  this.inputValue.push(data);
			},
			confirmHandle(value,id){
			    //点击键盘完成的回调函数
			    console.log(value)
				console.log("id=",id)
				var goodqty =value;
				var goodid =id;
			
				// 为0或减操作则查询
				var cart_item = null;
				var type =1;
				if(goodqty > 0) {
				    var cart_data = this.cart.data;
				    for(var i in cart_data) {
				        if(goodid == cart_data[i]['goods_id']) {
				            cart_item = cart_data[i];
				            break;
				        }
				    }
				}
				this.cart_update(cart_item.id,goodid,goodqty);
			},
			// 购物车状态
			cart_event(e) {
			    this.setData({
			        cart_status: !this.cart_status
			    });
			},

			buy_submit_event(){
				//回调父级的方法
				this.$emit('updateShopCarMethod',"");
			},
			buy_number_event(e) {
			    if(!app.globalData.is_single_page_check()) {
			        return false;
			    }
			    var user = app.globalData.get_user_info(this);
			    if (user != false) {
			        // 用户未绑定手机则转到登录页面
			        if (app.globalData.user_is_need_login(user)) {
			            uni.navigateTo({
			                url: "/pages/login/login?event_callback=buy_number_event"
			            });
			            return false;
			        } else {
			            var index = e.currentTarget.dataset.index;
			            var type = parseInt(e.currentTarget.dataset.type) || 0;
			            var temp_goods = this.data_list[index];
			            this.setData({goods_choose_data: temp_goods});

			            // 是否存在多规格
			            if((temp_goods.is_exist_many_spec || 0) != 0) {
			                // 是否购物车中操作
			                if(type == 0) {
			                    this.setData({
			                        cart_status: true
			                    });
			                    app.globalData.showToast('不同规格的商品需在购物车减购');
			                } else {
			                    if((this.$refs.goods_buy || null) != null) {
			                        this.$refs.goods_buy.init(temp_goods, {buy_event_type: 'cart'});
			                    }
			                }
			                return false;
			            }

			            // 数据操作处理
			            this.buy_number_event_handle(e, type, temp_goods);
			        }
			    }
			},
			// 列表数量事件处理
			buy_number_event_handle(e, type, goods, spec = '') {
			    var res = this.buy_number_handle(type, goods, 'buy_number');
			    if(res === false) {
			        return false;
			    }

			    // 为0或减操作则查询
			    var cart_item = null;
			    if(type == 0 || (type == 1 && goods['buy_number'] > 0)) {
			        var cart_data = this.cart.data;
			        var params_spec = ((spec || null) == null || typeof(spec) != 'object') ? '' : JSON.stringify(spec);
			        for(var i in cart_data) {
			            if(goods['id'] == cart_data[i]['goods_id']) {
			                var cart_spec = ((cart_data[i]['spec'] || null) == null || typeof(cart_data[i]['spec']) != 'object') ? '' : JSON.stringify(cart_data[i]['spec']);
			                if(type == 0 || (type == 1 && cart_spec == params_spec)) {
			                    cart_item = cart_data[i];
			                }
			                break;
			            }
			        }
			    }

			    // 数据临时记录
			    this.setData({
			        temp_opt_data: {
			            pos: e,
			            goods: goods,
			            type: type,
			        }
			    });

			    // 操作类型
			    if(res == 0) {
			        if(cart_item == null) {
			            app.globalData.showToast('购物车id有误');
			            return false;
			        }
			        this.cart_delete(cart_item.id);
			    } else if(cart_item == null) {
			        this.cart_save(goods, res, spec);
			    } else {
			        var number = (type == 0) ? (parseFloat(cart_item['stock'])-res).toFixed(2) : (res+parseFloat(cart_item['stock'])).toFixed(2);
			        this.cart_update(cart_item.id, goods['id'], number);
			    }
			    return true;
			},

			// 数量操作处理
			buy_number_handle(type, goods, buy_number_field) {
			    // 加减处理
			    var buy_number = goods[buy_number_field] || 0;
			    if(type == 0) {
			        buy_number -= 1;
			    } else {
			        buy_number += 1;
			    }
			    if(buy_number < 0) {
			        buy_number = 0;
			    }

			    // 数据校验
			    var buy_min_number = parseInt(goods['buy_min_number']) || 1;
			    var buy_max_number = parseInt(goods['buy_max_number']) || 0;
				var spec_buy_min_number = parseInt(goods['spec_buy_min_number']) || 0;
				var spec_buy_max_number = parseInt(goods['spec_buy_max_number']) || 0;
			    var inventory = parseInt(goods['inventory']);
			    var inventory_unit = goods['inventory_unit'];

				// 最小起购数量
				var min = (spec_buy_min_number > 0) ? spec_buy_min_number : buy_min_number;
				if (min > 0) {
				    if(type == 0) {
				        if(buy_number < min) {
				            buy_number = 0;
				        }
				    } else {
				        if(buy_number < min) {
				            buy_number = min;
				        }
				    }
				}

				// 最大购买数量
				var max = (spec_buy_max_number > 0) ? spec_buy_max_number : buy_max_number;
				if (max > 0 && buy_number > max) {
				    buy_number = max;
				    app.globalData.showToast('限购' + max + inventory_unit);
					return false;
				}

				// 数量是否改变
			    if (goods[buy_number_field] == buy_number) {
			        app.globalData.showToast('数量未改变');
			        return false;
			    }

			    // 操作数量
			    var opt_number = 1;
			    if(type == 0) {
			        if(buy_number <= 0) {
			            opt_number = 0;
			        }
			    } else {
			        if(buy_number > goods['buy_number']) {
			            opt_number = buy_number-goods['buy_number'];
			        }
			    }
			    return opt_number;
			},

			// 购物车添加
			cart_save(goods, buy_number, spec = '') {
			    uni.request({
			        url: app.globalData.get_request_url("cartSave"),
			        method: 'POST',
			        data: {
						"goodsId": goods['id'],
						"fqty": buy_number,
						"customerId" : this.customerId,
						"createBy" : this.user.username,
						"tenantId" :this.user.role.tenantId,
						"type" : this.user.userIdentity,
						"specifications": goods['model'],
						"unitId":goods['unitId'],
						"price":goods['price'],
						"unitName":goods['unitName'],
						"goodsName":goods['name']
					},
			    	header:{
			    		'Tenant-Id':this.user.role.tenantId,
			    		'X-Access-Token':this.user.token,
			    	},
			        dataType: 'json',
			        success: res => {
			            if (res.data.code == 200) {
			                this.cart_para_curve_handle();
			                this.get_cart_data();
			            } else {
			                if (app.globalData.is_login_check(res.data)) {
			                    app.globalData.showToast(res.data.msg);
			                }
			            }
			        },
			        fail: () => {
			            app.globalData.showToast('服务器请求出错');
			        }
			    });
			},
			// 搜索事件
			search_button_event(e) {
			    // 商品列表模式
			    if(this.category_show_level == 0) {
			        this.setData({
			            search_keywords_value: e || '',
			            data_page: 1,
			            data_list_loding_status: 1,
			            data_list: []
			        });
			        this.get_goods_list(1,1);
			    } else {
			        // 进入搜索页面
			        uni.navigateTo({
			            url: '/pages/goods-search/goods-search'+(((e || null) == null) ? '' : '?keywords='+e)
			        });
			    }
			},
			// 购物车抛物线动画
			cart_para_curve_handle() {
			    if((this.temp_opt_data || null) != null && (this.temp_opt_data.type || 0) == 1) {
			        if((this.$refs.cart_para_curve || null) != null) {
			            var self = this;
			            uni.createSelectorQuery().select('.botton-nav .cart').boundingClientRect().exec(function(res) {
			                self.$refs.cart_para_curve.init(res, self.temp_opt_data.pos, self.temp_opt_data.goods.images);
			            });
			        }
			    }
			},

			// 购物车更新
			cart_update(cart_id, goods_id, buy_number) {
				//console.log("--------cart_update--------")
			    uni.request({
			        url: app.globalData.get_request_url("cartUpdate"),
			        method: 'POST',
			        data: {
			            "id": cart_id,
			            "goodsId": goods_id,
			            "fqty": buy_number
			        },
					header:{
						'Tenant-Id':this.user.role.tenantId,
						'X-Access-Token':this.user.token,
					},
			        dataType: 'json',
			        success: res => {
			            if (res.data.code == 200) {
			                this.cart_para_curve_handle();
			                this.get_cart_data();
			            } else {
			                if (app.globalData.is_login_check(res.data)) {
			                    app.globalData.showToast(res.data.msg);
			                } else {
			                    app.globalData.showToast('提交失败，请重试！');
			                }
			            }
			        },
			        fail: () => {
			            app.globalData.showToast('服务器请求出错');
			        }
			    });
			},

			// 购物车删除
			cart_delete(cart_id) {
			    uni.request({
			        url: app.globalData.get_request_url("cartDelete"),
			        method: 'POST',
			        data: {
			            "id": cart_id
			        },
					header:{
						'Tenant-Id':this.user.role.tenantId,
						'X-Access-Token':this.user.token,
					},
			        dataType: 'json',
			        success: res => {
			            if (res.data.code == 200) {
			                this.get_cart_data();
			            } else {
			                if (app.globalData.is_login_check(res.data)) {
			                    app.globalData.showToast(res.data.msg);
			                } else {
			                    app.globalData.showToast('提交失败，请重试！');
			                }
			            }
			        },
			        fail: () => {
			            app.globalData.showToast('服务器请求出错');
			        }
			    });
			},


			openKeyBoard(e){
				
				var goodid =e.target.id;
				console.log("goodid=",goodid);
				this.ywid =goodid;
				//this.showDigitKeyboard =true;

				this.inputValue = [];
				this.keyboard_status =true;

			},
			// 弹层关闭
			popup_close_event(e) {
				this.setData({
					popup_status: false
				});
				uni.setStorageSync(this.cache_key, Date.parse(new Date()) / 1000);
			},
			open(customerId){
				this.customerId =customerId;
				this.popup_status =true;
				this.init();
			},
			close(){
				this.popup_status =false;
			},
			// 获取数据
			init() {
				this.user = app.globalData.get_user_info(this, "init")
			    if(this.is_first == 1) {
			        this.setData({
			            data_list_loding_status: 1
			        });
			    }

			    uni.request({
			        url: app.globalData.get_request_url("goodsCategory"),
			        method: 'GET',
			        data: {},
					header:{
						'Tenant-Id':this.user.role.tenantId,
						'X-Access-Token':this.user.token,
					},
			        dataType: 'json',
			        success: res => {
			            uni.stopPullDownRefresh();
						if(res.statusCode == 401){
							app.globalData.remove_user_cache_event_noToast();
							return;
						}
			            if (res.data.success) {
			                var data = res.data.result;
							var index = this.nav_active_index;
			                var temp_category = data.category || [];
			                // 全部分类子集数量
			                var category_one_subset_count = 0;
							// 是否指定分类
							var tabbar_params = this.tabbar_params;
							if(temp_category.length > 0) {
								for(var i in temp_category) {
			                        // 是否指定分类
									if((tabbar_params || null) != null && (tabbar_params.id || null) != null && temp_category[i]['id'] == tabbar_params.id) {
										index = i;
										break;
									}
			                        // 是否全部数据都无二级
			                        if((temp_category[i]['items'] || null) != null && temp_category[i]['items'].length > 0) {
			                            category_one_subset_count++;
			                        }
								}
							}
							// 设置分类及右侧数据和及基础数据
			                var upd_data = {
			                    category_list: temp_category,
			                    data_content: temp_category[index] || null,
								nav_active_index: index,
			                    category_one_subset_count: category_one_subset_count,
								plugins_label_data: (data.plugins_label_data || null) == null || (data.plugins_label_data.base || null) == null || (data.plugins_label_data.data || null) == null || data.plugins_label_data.data.length <= 0 ? null : data.plugins_label_data,
			                }
							// 指定分类则重新读取列表数据
							if(tabbar_params != null && this.nav_active_index != index) {
								upd_data['is_first'] = 1;
								upd_data['data_page'] = 1;
								upd_data['data_list'] = [];
								upd_data['data_list_loding_status'] = 1;
								upd_data['nav_active_item_two_index'] = -1;
								upd_data['nav_active_item_three_index'] = -1;
							}
			                // 非商品列表模式
			                if(this.category_show_level != 0) {
			                    upd_data['data_list_loding_status'] = temp_category.length == 0 ? 0 : 3;
			                    upd_data['data_bottom_line_status'] = true;
			                }
			                this.setData(upd_data);
			                // 商品列表模式
							app.globalData.showToast(this.category_show_level);
			                if(this.category_show_level == 0) {
			                    // 商品列表模式获取购物车数据
			                    this.get_cart_data();
			                    // 获取商品列表、仅首次请求商品列表
								app.globalData.showToast(this.is_first);
			                    if(this.is_first == 1) {
			                        this.get_goods_list(1,0);
			                    }
			                } else {
			                    // 分类模式下、仅首次请求购物车接口和商品模式下
			                    if(this.is_first == 1 && this.category_show_level == 0) {
			                        this.get_cart_data();
			                    }
			                }
							console.log("----------9")
			                // 是否首次记录
			                this.setData({is_first: 0});
			            } else {
			                this.setData({
			                    data_list_loding_status: 2,
			                    data_list_loding_msg: res.data.msg
			                });
			            }
			        },
			        fail: () => {
			            uni.stopPullDownRefresh();
			            this.setData({
			                data_list_loding_status: 2,
			                data_list_loding_msg: '服务器请求出错'
			            });
			            app.globalData.showToast('服务器请求出错');
			        }
			    });
			},
			// 获取商品列表
			get_goods_list(is_mandatory,is_search) {
			    // 分页是否还有数据
			    if ((is_mandatory || 0) == 0) {
			        if (this.data_bottom_line_status == true) {
			            uni.stopPullDownRefresh();
			            return false;
			        }
			    }

			    // 是否加载中
			    if(this.data_is_loading == 1) {
			        return false;
			    }
			    this.setData({data_is_loading: 1});

			    // 请求参数
			    var data = {
			        page: this.data_page,
			        wd: this.search_keywords_value || ''
			    };
			    // 分类id

			    if((this.data_content || null) != null) {
					if(is_search == 1){
						this.setData({
						    nav_active_index: this.category_list.length-1
						});
						//获取全部二级分类

						//this.data_content.items =[];
					}else{
						if(this.data_content['id'] == -1){

						}else{
							// 主分类id
							data['topcategoryId'] = this.data_content['id'];
						}

						// 是否选中了二级分类
						if(this.nav_active_item_two_index != -1) {
						   data['categoryId'] = this.data_content['items'][this.nav_active_item_two_index]['id'];
						}
						// 是否选中了三级分类
						if(this.data_three_content != null && this.nav_active_item_three_index != -1) {
						   data['categoryId'] = this.data_three_content['items'][this.nav_active_item_three_index]['id'];
						}
					}

			    }
				data['customerId'] =this.customerId;
				data['tenantId'] =this.user.role.tenantId;
				data['createBy'] =this.user.username;
			    // 获取数据
			    uni.request({
					//?pageNo=+this.data_page
			        url: app.globalData.get_request_url("getGoodsList?pageNo="+this.data_page),
			        method: 'POST',
			        data: data,
			    	header:{
			    		'Tenant-Id':this.user.role.tenantId,
			    		'X-Access-Token':this.user.token,
			    	},
			        dataType: 'json',
			        success: res => {

			            if (res.data.code == 0) {
			                var data = res.data.data;
							console.log("data=====",data)
			                if (data.data.length > 0) {
								console.log("1111=====",this.data_page)
			                    if (this.data_page <= 1) {
			                        var temp_data_list = data.data;
									console.log("temp_data_list=====",temp_data_list)
			                    } else {
			                        var temp_data_list = this.data_list || [];
			                        var temp_data = data.data;
			                        for (var i in temp_data) {
			                            temp_data_list.push(temp_data[i]);
			                        }
			                    }
			                    this.setData({
			                        data_list: temp_data_list,
			                        data_total: data.total,
			                        data_page_total: data.page_total,
			                        data_list_loding_status: 3,
			                        data_page: this.data_page + 1,
			                        data_is_loading: 0
			                    });

			                    // 是否还有数据
			                    this.setData({
			                        data_bottom_line_status: (this.data_page > 1 && this.data_page > this.data_page_total)
			                    });

			                    // 购物车数据处理
			                    this.cart_data_list_handle();
			                } else {
			                    this.setData({
			                        data_list_loding_status: 0,
			                        data_total: 0,
			                        data_is_loading: 0
			                    });
			                    if (this.data_page <= 1) {
			                        this.setData({
			                            data_list: [],
			                            data_bottom_line_status: false
			                        });
			                    }
			                }
			            } else {
			                this.setData({
			                    data_list_loding_status: 0,
			                    data_list_loding_msg: res.data.msg,
			                    data_is_loading: 0
			                });
			                app.globalData.showToast(res.data.msg);
			            }
			        },
			        fail: () => {
			            this.setData({
			                data_list_loding_status: 2,
			                data_list_loding_msg: '服务器请求出错',
			                data_is_loading: 0
			            });
			            app.globalData.showToast('服务器请求出错');
			        }
			    });
			},


			// 获取购物车数据
			get_cart_data() {
				if(this.user != null) {
					uni.request({
						url: app.globalData.get_request_url("getCartList"),
						method: 'POST',
						data: {
							"tenantId":this.user.role.tenantId,
							"customerId" : this.customerId,
							"createBy" : this.user.username,
						},
						header:{
							'Tenant-Id':this.user.role.tenantId,
							'X-Access-Token':this.user.token,
						},
						dataType: 'json',
						success: res => {
							if (res.data.code == 0) {
								var data = res.data.data;
								var temp_cart = data.data || [];
								this.setData({
									cart: res.data.data
								});
								this.cart_data_list_handle();
								console.log("----get_cart_data----",data.buy_number)
								// 导航购物车处理
								if (data.buy_number <= 0) {
									app.globalData.set_tab_bar_badge(2, 0);
								} else {
									app.globalData.set_tab_bar_badge(2, 1, data.buy_number);
								}
							}
						},
						fail: () => {
							app.globalData.showToast('服务器请求出错');
						}
					});
				}
			},

			// 购物车更新列表数据处理
			cart_data_list_handle() {
			    var temp_cart = this.cart || null;
				var total_price = 0;
			    if(temp_cart != null) {
					//console.log("temp_cart----not null");
			        var temp_data_list = this.data_list;
			        if(temp_data_list.length > 0) {
						//console.log("temp_data_list.length-----",temp_data_list.length);
			            for(var i in temp_data_list) {
			                temp_data_list[i]['buy_number'] = 0;
			                if(temp_cart.data.length > 0) {
			                    for(var k in temp_cart.data) {
			                        if(temp_cart.data[k]['goods_id'] == temp_data_list[i]['id']) {
			                            temp_data_list[i]['buy_number'] += parseFloat(temp_cart.data[k]['stock']);
			                        }
			                    }
			                }
			            }
			        }
			        this.setData({
			            data_list: temp_data_list
			        });
			    }
			},
			// 导航事件
			nav_event(e) {
			    var index = e.currentTarget.dataset.index;
			    var two_index = e.currentTarget.dataset.itemtwoindex;
			    var three_index = e.currentTarget.dataset.itemthreeindex;
			    var temp_data_content = this.category_list[index] || null;
			    var temp_data_three_content = null;
			    if(two_index != -1 && temp_data_content != null) {
			        temp_data_three_content = temp_data_content['items'][two_index];
			    }
			    this.setData({
			        nav_active_index: index,
			        nav_active_item_two_index: two_index,
			        nav_active_item_three_index: three_index,
			        data_content: temp_data_content,
			        data_three_content: temp_data_three_content,
			        data_page: 1,
			        data_list_loding_status: 1,
			        data_list: []
			    });

				//如果选了一级分类的全部
				// if(index == this.category_list.length-1){
				// }

			    // 商品模式则读取商品
			    if(this.category_show_level == 0) {
			        this.reset_scroll();
			        this.get_goods_list(1,0);
			    }
			},

			// 重置滑动位置
			reset_scroll() {
			    this.setData({
			        scroll_top: this.scroll_top_old
			    });
			    this.$nextTick(() => {
			        this.setData({
			            scroll_top: 0
			        });
			    });
			},

			// 滚动加载
			scroll_lower(e) {
			    this.get_goods_list();
			},

			// 滑动事件位置记录
			scroll_event(e) {
			    this.setData({
			        scroll_top_old: e.detail.scrollTop
			    });
			},
		}
	}
</script>

<style>
    @import '../../pages/goods-category/goods-category.css';
</style>
